package com.business.form.service;

import com.business.form.mapper.auto.FormInstanceFieldValueMapper;
import com.business.form.model.auto.FormInstanceFieldValue;
import com.business.form.model.auto.FormInstanceFieldValueExample;
import com.business.system.common.base.BaseService;
import com.business.system.common.support.Convert;
import com.business.system.model.custom.Tablepar;
import com.business.system.util.SnowflakeIdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 表单实例字段值 FormInstanceFieldValueService
 * @Title: FormInstanceFieldValueService.java 
 * @Package com.business.form.service 
 * @author LuJingyuan_自动生成
 * @email 674326842@qq.com
 * @date 2020-11-18 14:16:31  
 **/
@Service
public class FormInstanceFieldValueService implements BaseService<FormInstanceFieldValue, FormInstanceFieldValueExample>{
	@Autowired
	private FormInstanceFieldValueMapper formInstanceFieldValueMapper;
	
      	   	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<FormInstanceFieldValue> list(Tablepar tablepar,String name){
	        FormInstanceFieldValueExample testExample=new FormInstanceFieldValueExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andFieldLabelLike("%"+name+"%");
	        }

	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<FormInstanceFieldValue> list= formInstanceFieldValueMapper.selectByExample(testExample);
	        PageInfo<FormInstanceFieldValue> pageInfo = new PageInfo<FormInstanceFieldValue>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=Convert.toListStrArray(ids);
			FormInstanceFieldValueExample example=new FormInstanceFieldValueExample();
			example.createCriteria().andIdIn(lista);
			return formInstanceFieldValueMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public FormInstanceFieldValue selectByPrimaryKey(String id) {
				
			return formInstanceFieldValueMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(FormInstanceFieldValue record) {
		return formInstanceFieldValueMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(FormInstanceFieldValue record) {
				
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
			
				
		return formInstanceFieldValueMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(FormInstanceFieldValue record, FormInstanceFieldValueExample example) {
		
		return formInstanceFieldValueMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(FormInstanceFieldValue record, FormInstanceFieldValueExample example) {
		
		return formInstanceFieldValueMapper.updateByExample(record, example);
	}

	@Override
	public List<FormInstanceFieldValue> selectByExample(FormInstanceFieldValueExample example) {
		
		return formInstanceFieldValueMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(FormInstanceFieldValueExample example) {
		
		return formInstanceFieldValueMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(FormInstanceFieldValueExample example) {
		
		return formInstanceFieldValueMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param formInstanceFieldValue
	 * @return
	 */
	public int checkNameUnique(FormInstanceFieldValue formInstanceFieldValue){
		FormInstanceFieldValueExample example=new FormInstanceFieldValueExample();
		example.createCriteria().andFieldLabelEqualTo(formInstanceFieldValue.getFieldLabel());
		List<FormInstanceFieldValue> list=formInstanceFieldValueMapper.selectByExample(example);
		return list.size();
	}


	/**
	 * 根据表单实例id查询对应的表单填写的值
	 * @param formInstanceId
	 * @return
	 */
	public FormInstanceFieldValue selectformInstanceFieldValueByInstanceId(String formInstanceId,String formFieldName) {
		FormInstanceFieldValueExample example = new FormInstanceFieldValueExample();
		FormInstanceFieldValueExample.Criteria criteria = example.createCriteria();
		criteria.andFormInstanceIdEqualTo(formInstanceId);
		criteria.andFieldNameEqualTo(formFieldName);
		List<FormInstanceFieldValue> formInstanceFieldValues = formInstanceFieldValueMapper.selectByExample(example);
		return formInstanceFieldValues.size()>0 ? formInstanceFieldValues.get(0) : null;
	}
}
